import request from '@/plugins/axios'
import dayjs from 'dayjs'

let checkTime = 0
let userInfo: AuthCheckInfo = {} as any

export const checkLogin = async (): Promise<AuthCheckInfo> => {
  const now = dayjs().unix()
  if (checkTime >= now) {
    return userInfo
  }
  // 增加60秒的阈值限制频繁请求验证
  checkTime = now + 60
  try {
    const resp = await request({
      url: 'admin/check_login'
    })
    userInfo = resp
  } catch (e) {
    userInfo.ok = false
  }
  return userInfo
}

// 后台用户登陆
export const adminLogin = async (data: AuthAdminLogin): Promise<AuthLoginResponse> => request({
  url: 'admin/login',
  method: 'post',
  data
})

// 后台用户密码修改
export const adminEditPwd = async (data: AuthEditPassForm) => request({
  url: 'admin/edit_pwd',
  method: 'post',
  data
})

// 注销登陆
export const adminLogout = async (): Promise<void> => request({
  url: 'admin/logout',
  method: 'post'
})
